package com.emode.action;

import java.io.*;
import java.util.*;

import net.sf.cglib.core.TinyBitSet;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.emode.base.BaseAction;
import com.emode.entity.Bbsreply;
import com.emode.entity.Bbsusers;
import com.emode.util.CommonUtils;
import com.emode.util.FileUtil;
import com.opensymphony.xwork2.ActionContext;
@SuppressWarnings("unchecked")
public class UserAction extends BaseAction<Bbsusers> implements SessionAware{
	/*
	 * 
	 */
	private static final long serialVersionUID = -3556586124161294936L;
	private File[] file;
	private String[] fileFileName;
	private String[] fileContentType;
	
	
	 //Struts2中Map类型的session
	Map<String, Object> session;
    //接收客户端传来的验证码
    private String securityCode;
	
	//跳转 login
	public String tologin(){
		return "tologin";
		
	}
	//跳转 toreg
	public String toreg(){
		return "toreg";
		
	}
	//用户登录
	 
	public String login()throws Exception{
		if(null==model.getUname()||null==model.getUpassword()){
			return "tologin";
		}
		Bbsusers user = userservice.find(model);
		if(user == null){
			msg = "用户名或密码错误，请重新输入";
			return "tologin";
		}
		else {
			//得到系统当前的时间
			if(4==user.getUstate()){
				msg = "该账户被封禁";
				return "tologin";
			}
			user.setLastlogin(new java.sql.Timestamp(new java.util.Date().getTime()));
			userservice.updateLoginDate(user);
			ActionContext.getContext().getSession().put("user", user);
			return "sectionList";
		}
	}
	//ajax 校验
	public String checkLogin()throws Exception{
		ServletActionContext.getResponse().setContentType(
				"text/javascript;charset=UTF-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		Bbsusers user = userservice.find(model);
		if(user==null){
			msg="false";
			out.print(msg);
		}else{
			msg="true";
			out.print(msg);
		}
		return null;
	}
	//退出
	public String logout(){
		ActionContext.getContext().getSession().remove("user");
		return "logout";
	}
	/*
	 * 密码md5 
	 * 邮箱激活
	 */
		//用户注册
		public String reg() throws IOException{
			System.out.println(model.getUname());
			Bbsusers user = userservice.find(model);
			if(user!=null){
				msg="该用户名已存在,请重新输入";
				return null;
			}else{
				String passwordMD5 = DigestUtils.md5Hex(model.getUpassword());
				model.setUpassword(passwordMD5);
				model.setActivationCode(CommonUtils.uuid()+CommonUtils.uuid());
				model.setUregDate(new Date());
				userservice.save(model);
				msg = "注册成功，请登录!";
				return "tologin";	
			}
		}
		
	//用户校验
	public String ValidateName() throws Exception {
		return getResult();
		
	}
	//邮箱校验
	public String ValidateEmail() throws Exception {
		return getResult();
		
	}
	public String getResult() throws IOException {
		ServletActionContext.getResponse().setContentType(
				"text/javascript;charset=UTF-8");
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		Bbsusers user = userservice.find(model);
		if(user!=null){
			msg="false";
			out.print(msg);
		}else{
			msg="true";
			out.print(msg);
		}
		return null;
	}
		
	//验证码校验
	public String ValidateCode() throws Exception{
		 ServletActionContext.getResponse().setContentType(
				"text/javascript;charset=UTF-8");
		 PrintWriter out = ServletActionContext.getResponse().getWriter();
		 String serverCode = (String)session.get("SESSION_SECURITY_CODE");
		 if(!serverCode.equals(securityCode)){
			 msg = "false";
			 out.print(msg);
		 }else{
			 msg="true";
			 out.print(msg);
		 }
		return null;
	}
	
	//list 显示所有用户
	
	public String all() throws Exception{
		System.out.println("allusers");
		List<Bbsusers> users = userservice.findAll();
		ActionContext.getContext().getSession().put("users", users);
		return "allusers";
	}
	
	// 用户详情
	public String userDetails()throws Exception{
		System.out.println("start");
		Bbsusers user = userservice.getById(model.getUid());
		list = topicservice.findTopicByUid(model.getUid());
		String whereHql = " and bbsusers.uid = ? order by o.rtime ";
		Object [] values = {model.getUid()};
		List<Bbsreply> replyList = replyservice.findObjectByConditionWithNoPage(whereHql, values);
		ActionContext.getContext().put("u", user);
		ActionContext.getContext().put("list2", list);
		ActionContext.getContext().put("reply", replyList);
		return "userDetail";
		
	}
	//通过uname查找用户
	public String searchUser() throws Exception{
		list = userservice.findUsers(model.getUname());
		if(list.size()>0){
			return "searchresult";
		}else{
			msg = "没有找到搜索结果";
			return msg;
		}
		
	}

	
	//用户 设置
	public String setProfile() throws Exception{
		model.setUid(getCurrentUser().getUid());
		Bbsusers user  =  userservice.getById(model.getUid());
		ActionContext.getContext().put("u", user);
		return "profile";
		
	}
	//更新profile
	public String update(){
		Bbsusers user = getCurrentUser();
		user.setUemail(model.getUemail());
		user.setUremark(model.getUremark());
		user.setUremark(model.getHomepage());
		userservice.update(user);
		return "profile";
		
	}
	
	public String setAvatar(){
		return "setavatar";
		
	}
	//跳转到设置密码页面
	public String setPsw(){
		return "setpsw";
	}
	//跳转到忘记密码页面
	public String to_forgetpsw(){
		return "toforgetpsw";
	}
	/*
	 * 修改密码 得到当前的密码，和系统密码对比，
	 * 当一致是修改
	 */
	public String confirmPsw(){
		
		//System.out.println(getCurrentUser().getUpassword());
		String pass = DigestUtils.md5Hex(model.getUpassword());
		if(getCurrentUser().getUpassword().equals(pass)){
			Bbsusers user = getCurrentUser();
			user.setUpassword(DigestUtils.md5Hex(model.getRepsw()));
			userservice.update(user);
			msg="密码修改成功";
			return "setpsw";
		}else{
			msg="您输入的密码不正确！";
			return "setpsw";
		}
	}
	//ajax 检查密码
	public String checkPsw() throws IOException{
		return getResult();
	}
	/*
	 * 上传头像  上传的路径为工程的webapp下面，tomcat重启会删除upload
	 */
	public String upAvatar() throws IOException{
		String photopath = "upload\\avatar\\";
		for(int i=0;i<file.length;i++){
			if(file[i].length()>0){
				String filePath = FileUtil.fileCopy(file[i], fileFileName[i],photopath);
				filePath = filePath.replace("\\", "/");
				System.out.println(filePath);
				Bbsusers user =getCurrentUser();
				user.setAvatar(filePath);	
				userservice.update(user);
			}
		}
		return "profile";
		
	}
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	public String getSecurityCode() {
		return securityCode;
	}
	public void setSecurityCode(String securityCode) {
		this.securityCode = securityCode;
	}
	
	
	public File[] getFile() {
		return file;
	}
	public void setFile(File[] file) {
		this.file = file;
	}


	public String[] getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String[] fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String[] getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String[] fileContentType) {
		this.fileContentType = fileContentType;
	}
}
